package com.zyq.shoppinglist;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

/**
 * Created by Zyq On 2019/4/3 10:56
 */
public class ShopAdapter extends BaseExpandableListAdapter {
    private Context mcontext;
    private List<ShopEntity.DataBean> group;

    public ShopAdapter(Context mcontext, List<ShopEntity.DataBean> group) {
        this.mcontext = mcontext;
        this.group = group;
    }

    public List<ShopEntity.DataBean> getList(){
        return group;
    }
    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return group.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return group.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        MyGroupViewHolder holder1 = null;
        if (convertView == null) {
            holder1 = new MyGroupViewHolder();
            convertView = View.inflate(mcontext, R.layout.item_parent, null);
            holder1.group_checkBox = convertView.findViewById(R.id.group_checkBox);
            holder1.group_name = convertView.findViewById(R.id.group_name);
            convertView.setTag(holder1);
        } else {
            holder1 = (MyGroupViewHolder) convertView.getTag();
        }
        ShopEntity.DataBean dataBean = group.get(groupPosition);
        holder1.group_checkBox.setChecked(dataBean.isChecked());
        holder1.group_name.setText(dataBean.getSellerName());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        MyChildViewHolder holder2 = null;
        if (convertView == null) {
            holder2 = new MyChildViewHolder();
            convertView = View.inflate(mcontext, R.layout.item_son, null);
            holder2.child_checkBox = convertView.findViewById(R.id.child_checkBox);
            holder2.child_iamge = convertView.findViewById(R.id.child_iamge);
            holder2.child_title = convertView.findViewById(R.id.child_title);
            holder2.child_price = convertView.findViewById(R.id.child_price);
            holder2.myAddSub = convertView.findViewById(R.id.myAddSub);
            convertView.setTag(holder2);

        } else {
            holder2 = (MyChildViewHolder) convertView.getTag();
        }
        final ShopEntity.DataBean.ListBean listBean = group.get(groupPosition).getList().get(childPosition);
        holder2.child_checkBox.setChecked(listBean.isChecked());
//        Glide.with(mcontext).load(listBean.getImages().split("\\|")).into(holder2.child_iamge);
//        Picasso.with(mcontext).load(listBean.getImages().split("\\|")[0]).into(holder2.child_iamge);
        holder2.child_title.setText(listBean.getTitle());
        holder2.child_price.setText(listBean.getPrice() + "");
        holder2.myAddSub.setCurrentNum(listBean.getNum());

        holder2.myAddSub.setOnChangNumListener(new AddAndSub.OnChangNum() {
            @Override
            public void changeNum(View view, int currentNum) {
                listBean.setNum(currentNum);
                onChagenum.change(view, currentNum);
            }
        });
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    class MyGroupViewHolder {
        CheckBox group_checkBox;
        TextView group_name;
    }

    class MyChildViewHolder {
        CheckBox child_checkBox;
        ImageView child_iamge;
        TextView child_title, child_price;
        AddAndSub myAddSub;
    }

    public interface OnChagenum {
        void change(View view, int num);
    }

    private OnChagenum onChagenum;

    public void setOnChagenum(OnChagenum onChagenum) {
        this.onChagenum = onChagenum;
    }
}


